package com.muse;

/**
 * @description 输入值为x，返回值y时: 满足 y = 2^k >= x，k取最小值时。
 * @author: muse
 **/
public class TableSizeForTest {
    private static final int MAXIMUM_CAPACITY = 1 << 30;

    public static void main(String[] args) {
        // test1();
        test2();
    }

    public static void test1() {
        int n = 129;
        System.out.println("n:  " + Integer.toBinaryString(n));
        int x = n >>> 1;
        System.out.println("x:  " + Integer.toBinaryString(x));
        n |= n >>> 1;
        System.out.println("n |= n >>> 1  :" + Integer.toBinaryString(n));
        n |= n >>> 2;
        System.out.println("n |= n >>> 2  :" + Integer.toBinaryString(n ));
        n |= n >>> 4;
        System.out.println("n |= n >>> 4  :" + Integer.toBinaryString(n));
        n |= n >>> 8;
        System.out.println("n |= n >>> 8  :" + Integer.toBinaryString(n));
        n |= n >>> 16;
        System.out.println("n |= n >>> 16 :" + Integer.toBinaryString(n));
    }

    public static void test2() {
        System.out.println("input=3, output=" + tableSizeFor(3));
        System.out.println("input=4, output=" + tableSizeFor(4));
        System.out.println("input=5, output=" + tableSizeFor(5));
        System.out.println("input=11, output=" + tableSizeFor(11));
        System.out.println("input=12, output=" + tableSizeFor(12));
        System.out.println("input=13, output=" + tableSizeFor(13));
        System.out.println("input=200, output=" + tableSizeFor(200));
        System.out.println("input=210, output=" + tableSizeFor(210));
        System.out.println("input=220, output=" + tableSizeFor(220));
    }

    private static final int tableSizeFor(int c) {
        int n = c - 1;
        n |= n >>> 1;
        n |= n >>> 2;
        n |= n >>> 4;
        n |= n >>> 8;
        n |= n >>> 16;
        return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
    }
}
